﻿#include <iostream>
#include "threadpool.h"
#include <thread>
using namespace std;

/*
example:
	ThreadPool pool;
	pool.start(4);

	class Mytask : public Task
	{
		public:
			void run()
			{   
			}
	};

	pool.submitTask(std::make_shared<Mytask>());
*/

class Mytask : public Task
{
public:
	Mytask(int begin, int end)
		:begin_(begin)
		, end_(end)
	{ }
	//问题1：怎样设计run函数返回值可以返回不同类型？
	//Java Python Object 是所有类型的基类
	//C++17 Any类型
	Any run()
	{
		std::cout << "tid:" << std::this_thread::get_id()
			<< "begin!"<<std::endl;
		//std::this_thread::sleep_for(std::chrono::seconds(5));
		for (int i = begin_; i <= end_; i++)
			sum += i;
		std::cout << "tid:" << std::this_thread::get_id()
			<< "end!" << std::endl;
		return sum;

	}
private:
	int begin_;
	int end_;
	int sum;
};


int main()
{
	ThreadPool pool;
	pool.start(4);
	//问题2：怎样设计Result机制？
	//提交任务的线程和执行任务的线程（线程池分配）不是一个
	Result res = pool.submitTask(std::make_shared<Mytask>(1,10000000)); //提交任务
	int sum=res.get().cast_<int>();  //返回了一个Any类型，怎样转为具体类型？


	//用户自己return返回值，可以确定类型

	//pool.submitTask(std::make_shared<Mytask>());
	//pool.submitTask(std::make_shared<Mytask>());
	//pool.submitTask(std::make_shared<Mytask>());
	//pool.submitTask(std::make_shared<Mytask>());
	//pool.submitTask(std::make_shared<Mytask>());
	//pool.submitTask(std::make_shared<Mytask>());
	//pool.submitTask(std::make_shared<Mytask>());
	//pool.submitTask(std::make_shared<Mytask>());
	//pool.submitTask(std::make_shared<Mytask>());
	//pool.submitTask(std::make_shared<Mytask>());
	//pool.submitTask(std::make_shared<Mytask>());
	//pool.submitTask(std::make_shared<Mytask>());

	getchar();
}